package com.example.calculation;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.dao.Dao;
import java.util.HashMap;
import java.util.Map;

public class DaoImp implements Dao {
    @Override
    public void add(String content) {

        DBOpenHelper dbsqLiteOpenHelper = new DBOpenHelper(null,"users.db",null,1);
        SQLiteDatabase db = dbsqLiteOpenHelper.getWritableDatabase();
        //创建存放数据的ContentValues对象
        ContentValues values = new ContentValues();
        values.put("content",content);
        //数据库执行插入命令
        db.insert("user", null, values);

    }

    @Override
    @SuppressLint("Range")
    public Map<Integer, String> selectAll() {
        DBOpenHelper dbsqLiteOpenHelper = new DBOpenHelper(null, "users.db", null, 1);
        SQLiteDatabase db = dbsqLiteOpenHelper.getWritableDatabase();
        Map<Integer, String> m = new HashMap<>();

        //创建游标对象
        Cursor cursor = db.query("user", new String[]{"id", "username", "age"}, "username=?",
                new String[]{"admin123"}, null, null, null);
        //利用游标遍历所有数据对象
        while (cursor.moveToNext()) {
            Integer id = cursor.getInt(cursor.getColumnIndex("id"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            m.put(id, content);
        }
        // 关闭游标，释放资源
        cursor.close();
        return m;
    }
}
